import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-09-16
 * Time:9:06
 */
//<>哪个类具有了这个能力

    //Comparable  有什么不好的地方
//对类的侵入性比较强，每次如果更改比较方法需要改变类的内部。局限性很大。
class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getScore() {
        return score;
    }

    private double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

}
class AgeComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge() - o2.getAge();
    }
}
//class Student implements Comparable<Student>{
//    private String name;
//    private int age;
//    private double score;
//
//    public Student(String name, int age, double score) {
//        this.name = name;
//        this.age = age;
//        this.score = score;
//    }
//
//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                ", score=" + score +
//                '}';
//    }
////比较规则
//    @Override
//    public int compareTo(Student o) {
//        return (int) (this.score - o.score);
////return this.name.compareTo(o.name);
//       // return o.age - this.age;
////        if (this.age > o.age){
////            return 1;
////        }else if(this.age == o.age){
////            return 0;
////        }else {
////            return -1;
////        }
//    }
//}

class NameComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getName().compareTo(o2.getName());
    }
}

class ScoreComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.getScore() - o2.getScore());
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("gb",28,99.9);
        students[1] = new Student("bit",18,19.9);
        students[2] = new Student("ang",98,69.9);
        System.out.println("排序前：" + Arrays.toString(students));

        AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(students,scoreComparator);
        System.out.println("排序后：" + Arrays.toString(students));
    }
    public static void main3(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("gb",28,99.9);
        students[1] = new Student("bit",18,19.9);
        students[2] = new Student("ang",98,69.9);
        System.out.println("排序前：" + Arrays.toString(students));

        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students,ageComparator);
        System.out.println("排序后：" + Arrays.toString(students));

        //Comparable<Student> comparable = new Student("gb",28,99.9);
    }
    public static void main2(String[] args) {
        Student student = new Student("bit",19,90.9);
        System.out.println(student);
    }
    public static void main1(String[] args) {
        int[] array = {19,3,2,18,7,4};
        System.out.println(Arrays.toString(array));
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
    }
}
